// pages/prescription-select/prescription-select.js
Page({
  data: {
    patientName: '',
    department: '',
    subDepartment: '',
    campus: '',
    refillablePrescriptions: []
  },

  onLoad(options) {
    if (options.patient) this.setData({ patientName: options.patient })
    if (options.department) this.setData({ department: options.department })
    if (options.subDepartment) this.setData({ subDepartment: options.subDepartment })
    if (options.campus) this.setData({ campus: options.campus })

    this.loadRefillable()
  },

  // 载入可配药处方（从查看处方页的同一数据源中过滤 status === '待配药'）
  loadRefillable() {
    const { department, subDepartment } = this.data
    const list = this.getPrescriptionsByDepartment(department, subDepartment)
    const refillable = list.filter(item => item.status === '待配药')
    this.setData({ refillablePrescriptions: refillable })
  },

  getPrescriptionsByDepartment(department, subDepartment) {
    // 与 pages/view-prescription/view-prescription.js 保持一致并补充多条“待配药”数据
    const prescriptions = {
      '内科': {
        '呼吸内科': [
          { id: '002', date: '2025-07-15', status: '待配药', doctor: '张医生', department: '内科 - 呼吸内科', diagnosis: '支气管炎' },
          { id: '014', date: '2025-08-06', status: '待配药', doctor: '张医生', department: '内科 - 呼吸内科', diagnosis: '上呼吸道感染' },
          { id: '015', date: '2025-08-08', status: '待配药', doctor: '刘医生', department: '内科 - 呼吸内科', diagnosis: '急性咽炎' }
        ],
        '心血管内科': [
          { id: '005', date: '2025-08-05', status: '待配药', doctor: '王医生', department: '内科 - 心血管内科', diagnosis: '高血压' },
          { id: '016', date: '2025-08-08', status: '待配药', doctor: '王医生', department: '内科 - 心血管内科', diagnosis: '冠心病随访' }
        ],
        '消化内科': [
          { id: '003', date: '2025-08-02', status: '已完成', doctor: '李医生', department: '内科 - 消化内科', diagnosis: '慢性胃炎' }
        ]
      },
      '外科': {
        '普通外科': [ { id: '009', date: '2025-08-04', status: '待配药', doctor: '钱医生', department: '外科 - 普通外科', diagnosis: '甲状腺结节' } ]
      },
      '皮肤科': {
        '皮肤科门诊': [
          { id: '012', date: '2025-08-07', status: '待配药', doctor: '周医生', department: '皮肤科 - 皮肤科门诊', diagnosis: '湿疹' },
          { id: '017', date: '2025-08-08', status: '待配药', doctor: '周医生', department: '皮肤科 - 皮肤科门诊', diagnosis: '皮炎' },
          { id: '013', date: '2025-07-30', status: '已完成', doctor: '周医生', department: '皮肤科 - 皮肤科门诊', diagnosis: '荨麻疹' }
        ]
      }
    }
    if (!department || !subDepartment) return []
    return (prescriptions[department] && prescriptions[department][subDepartment]) || []
  },

  goRefill(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url:
        '/pages/online-refill/online-refill?patient=' + this.data.patientName +
        '&prescriptionId=' + id +
        '&department=' + this.data.department +
        '&subDepartment=' + this.data.subDepartment +
        '&campus=' + this.data.campus
    })
  },

  viewDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url:
        '/pages/prescription-detail/prescription-detail?prescriptionId=' + id + '&patient=' + this.data.patientName
    })
  }
})


